package com.resifind.www.service.impl;

import com.resifind.www.constant.*;
import com.resifind.www.dao.impl.BlacklistDaoImpl;
import com.resifind.www.dao.impl.FavourDaoImpl;
import com.resifind.www.dao.impl.OrderDaoImpl;
import com.resifind.www.exception.*;
import com.resifind.www.po.Client;
import com.resifind.www.po.Merchant;
import com.resifind.www.po.Order;
import com.resifind.www.service.BlacklistFavourService;
import com.resifind.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 36069
 */
public class BlacklistFavourServiceImpl implements BlacklistFavourService {
    private final BlacklistDaoImpl blacklistDao = new BlacklistDaoImpl();
    private final FavourDaoImpl favourDao = new FavourDaoImpl();
    private final OrderDaoImpl orderDao = new OrderDaoImpl();

    @Override
    public Resp<String> blackMerchant(int clientId, int merchantId) {
        //判断客户是否已经拉黑该商家
        if (blacklistDao.queryBlacklistExist(clientId, merchantId, BlacklistTypes.CLINT_LIST)) {
            throw BlacklistAndFavourException.blacklistAlreadyExists();
        } else {
            Connection conn = null;
            try {
                conn = JdbcUtils.getConn();
                JdbcUtils.beginTransaction(conn);
                boolean blackResult = blacklistDao.addBlacklist(clientId, merchantId, BlacklistTypes.CLINT_LIST, conn);
                if (blackResult) {
                    JdbcUtils.commitTransaction(conn);
                    return Resp.success("success");
                } else {
                    JdbcUtils.rollbackTransaction(conn);
                    throw new BaseException(ErrorCode.DB_ERROR);
                }
            } catch (BaseException e) {
                throw e;
            } catch (Exception e) {
                JdbcUtils.rollbackTransaction(conn);
                throw new BaseException(ErrorCode.SERVER_ERROR);
            } finally {
                JdbcUtils.closeConn(conn, null, null);
            }
        }
    }

    @Override
    public Resp<String> blackClient(int clientId, int merchantId) {
        // 检查客户是否已经被该商家拉黑
        if (blacklistDao.queryBlacklistExist(clientId, merchantId, BlacklistTypes.MERCHANT_LIST)) {
            throw BlacklistAndFavourException.blacklistAlreadyExists();
        }
        List<Order> orders = orderDao.queryOrderByUserId(clientId, UserTypes.CLIENT);
        if (!orders.isEmpty()) {
            for (Order order : orders) {
                if (order.getMerchantId() != merchantId) {
                    continue;
                }
                boolean flag = (order.getTradeStatus().equals(TradeStatus.PAID) && !order.getCheckStatus().equals(CheckStatus.CHECKED_OUT))
                        || order.getTradeStatus().equals(TradeStatus.REFUNDING)
                        || order.getTradeStatus().equals(TradeStatus.UNPAID);
                if (flag) {
                    //有已支付但不是已退房, 或退款中\未付款的订单, 不允许拉黑
                    throw OrderException.orderNotComplete();
                }
            }
        }
        Connection conn = null;
        try {
            conn = JdbcUtils.getConn();
            JdbcUtils.beginTransaction(conn);
            boolean addBlacklistResult = blacklistDao.addBlacklist(clientId, merchantId, BlacklistTypes.MERCHANT_LIST, conn);
            if (addBlacklistResult) {
                JdbcUtils.commitTransaction(conn);
                return Resp.success("success");
            } else {
                JdbcUtils.rollbackTransaction(conn);
                throw new BaseException(ErrorCode.DB_ERROR);
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            JdbcUtils.rollbackTransaction(conn);
            throw new BaseException(ErrorCode.SERVER_ERROR);
        } finally {
            JdbcUtils.closeConn(conn, null, null);
        }
    }

    @Override
    public Resp<List<Merchant>> queryBlackList(int clientId) {
        //查询客户的黑名单
        List<Merchant> merchants = blacklistDao.queryBlackListByClientId(clientId);
        return Resp.success(merchants);
    }

    @Override
    public Resp<List<Client>> queryBlacklist(int merchantId) {
        //查询商家的黑名单
        List<Client> clients = blacklistDao.queryBlacklistByMerchantId(merchantId);
        return Resp.success(clients);
    }

    @Override
    public Resp<String> cancelBlack(int id, List<Integer> ids, String userType) {
        Connection conn = null;
        try {
            conn = JdbcUtils.getConn();
            JdbcUtils.beginTransaction(conn);
            boolean dropBlacklistResult;
            if (userType.equals(UserTypes.CLIENT)) {
                dropBlacklistResult = blacklistDao.deleteClientBlacklist(id, ids, conn);
            } else if (userType.equals(UserTypes.MERCHANT)) {
                dropBlacklistResult = blacklistDao.deleteMerchantBlacklist(id, ids, conn);
            } else {
                throw UserException.userTypeError();
            }
            if (dropBlacklistResult) {
                JdbcUtils.commitTransaction(conn);
                return Resp.success("success");
            } else {
                JdbcUtils.rollbackTransaction(conn);
                throw new BaseException(ErrorCode.DB_ERROR);
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            JdbcUtils.rollbackTransaction(conn);
            throw new BaseException(ErrorCode.SERVER_ERROR);
        } finally {
            JdbcUtils.closeConn(conn, null, null);
        }
    }

    @Override
    public Resp<String> favourMerchant(int clientId, int merchantId) {
        //判断客户是否已经收藏该商家
        if (favourDao.queryFavourExist(clientId, merchantId)) {
            throw BlacklistAndFavourException.favourAlreadyExists();
        }
        Connection conn = null;
        try {
            conn = JdbcUtils.getConn();
            JdbcUtils.beginTransaction(conn);
            //收藏商家
            boolean favourResult = favourDao.favourMerchant(clientId, merchantId, conn);
            if (favourResult) {
                JdbcUtils.commitTransaction(conn);
                return Resp.success("success");
            } else {
                JdbcUtils.rollbackTransaction(conn);
                throw new BaseException(ErrorCode.DB_ERROR);
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            JdbcUtils.rollbackTransaction(conn);
            throw new BaseException(ErrorCode.SERVER_ERROR);
        } finally {
            JdbcUtils.closeConn(conn, null, null);
        }
    }

    @Override
    public Resp<List<Merchant>> queryFavourMerchant(int clientId) {
        //查询客户收藏的商家
        List<Merchant> merchants = favourDao.queryFavourMerchant(clientId);
        return Resp.success(merchants);
    }

    @Override
    public Resp<String> cancelFavour(int clientId, int merchantId) {
        Connection conn = null;
        try {
            conn = JdbcUtils.getConn();
            JdbcUtils.beginTransaction(conn);
            boolean cancelFavourResult = favourDao.dropFavour(clientId, merchantId, conn);
            if (cancelFavourResult) {
                JdbcUtils.commitTransaction(conn);
                return Resp.success("success");
            } else {
                JdbcUtils.rollbackTransaction(conn);
                throw new BaseException(ErrorCode.DB_ERROR);
            }
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            JdbcUtils.rollbackTransaction(conn);
            throw new BaseException(ErrorCode.SERVER_ERROR);
        } finally {
            JdbcUtils.closeConn(conn, null, null);
        }
    }
}
